package com.example.demofdfs.net;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.Map;
import java.util.function.BiConsumer;

public class HttpUtil {

	
	public static String get(String url, Map<String, String> headers) throws IOException {
		URLConnection connect = new URL(url).openConnection();
		HttpURLConnection http = (HttpURLConnection) connect;
		
		http.setRequestMethod("GET");
		http.setRequestProperty("X-Token", "eyJhbGciOiJSUzI1NiJ9.eyJpYXQiOjE1NDU3MDczMjksImV4cCI6MTU0NTc5MzcyOSwiaXAiOiJ4eHgiLCJ1c2VySWQiOiIxIn0.aUYUa80jIfyPh7pkCckG-D2rW86z7KRdMrlSvPfM6tJnG39nWKAWj6u6b14Iu9mri5uRHdqL77Yhd5iTRtNrpe7_MVdk9IaKJVXQdzlTDr-D3fjzsIt0cJShWSzbrk2cz7urEGtUw5t3Lua9z_Vg7WlfUXcIJlSTcNSWZz5QP-4");
//		http.setRequestProperty("userName", "admin" );
//		http.setRequestProperty("userId", "1" );
//		http.setRequestProperty("cookie", "Admin-Token=eyJhbGciOiJSUzI1NiJ9.eyJpYXQiOjE1NDU3MDczMjksImV4cCI6MTU0NTc5MzcyOSwiaXAiOiJ4eHgiLCJ1c2VySWQiOiIxIn0.aUYUa80jIfyPh7pkCckG-D2rW86z7KRdMrlSvPfM6tJnG39nWKAWj6u6b14Iu9mri5uRHdqL77Yhd5iTRtNrpe7_MVdk9IaKJVXQdzlTDr-D3fjzsIt0cJShWSzbrk2cz7urEGtUw5t3Lua9z_Vg7WlfUXcIJlSTcNSWZz5QP-4; userId=1; userName=admin");
//		http.setRequestProperty("user-anget", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36");
		
		http.connect();
		InputStream in = http.getInputStream();
		
		byte[] b = new byte[1024];
		int len;
		StringBuffer sb = new StringBuffer();
		while ((len = in.read(b)) != -1) {
			sb.append(new String(b, 0, len, "UTF-8"));
		}

		http.getHeaderFields().forEach(new BiConsumer<String, List<String>>() {

			@Override
			public void accept(String t, List<String> u) {
				System.out.println(t + " : " + u);
			}
			
		});
		return sb.toString();
		
	}
	
	public static String post(String url, Map<String, String> headers) {
		return url;
	}
}
